<template>
  <div class="monitor_page">
    <div class="monitor_page-filter">
      <el-form ref="form" label-width="100px" :inline="true">
        <el-form-item label="起止时间：">
          <el-date-picker
            v-show="!isToggleStatus"
            style="width:100%;min-width:240px"
            size="small"
            value-format="timestamp"
            v-model="timerArray"
            type="datetimerange"
            :picker-options="pickerOptions"
            :clearable="false"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
      </el-form>
    </div>
    <div class="monitor_page-filter flex-between">
      <div class="monitor_page-filter-left">
        <el-button
          @click="confirmDelete"
          icon="el-icon-delete-solid"
          v-if="hasPermission('monitor:record:del')"
          type="danger">删除
        </el-button>
        <el-button
          plain
          v-if="hasPermission('monitor:record:clean')"
          icon="el-icon-s-tools"
          type="primary"
          @click="confirmCleanBaseRecord">设备重置
        </el-button>
        <el-button
          plain
          v-if="hasPermission('monitor:record:mark')"
          type="primary"
          @click="showMonitorRemarkDialog">说明
        </el-button>
        <el-button
          style="margin-top: 10px"
          plain
          v-if="jiuGanKeJi"
          type="primary"
          @click="lookEquipRecord(timerArray,equipId)">运行日志
        </el-button>
        <el-button
          plain
          v-if="hasPermission('monitor:record:export')"
          @click="$emit('export')"
          icon="el-icon-download"
          type="primary">数据导出
        </el-button>
        <el-button
          type="primary"
          class="common_btn-defalut"
          icon="el-icon-search"
          @click="form.page=1;$emit('update')">搜索
        </el-button>
      </div>
    </div>

    <div style="width: 100%;padding-left: 10px">
      <el-tabs type="border-card" v-model="activeName2" @tab-click="clickTab">
        <el-tab-pane label="数据表格" name="1">
          <span slot="label"><i class="el-icon-date"></i> 数据表格</span>
          <el-table
            v-show="!isToggleStatus"
            :data="tableList"
            border
            @selection-change="handleSelectionChange"
            ref="multipleTable"
            tooltip-effect="dark">
            <el-table-column type="selection" width="28">
            </el-table-column>
            <el-table-column prop="createTime" label="数据采集时间" min-width="135">
              <template slot-scope="scope">
                {{scope.row.createTime | formatTimer }}
              </template>
            </el-table-column>
            <el-table-column v-for="(item,index) in tabsName" :key="index" :label="item" prop="dataList">
              <template slot-scope="scope">
                <span>{{scope.row.dataList[index+1].relativeValue}}</span>
              </template>
            </el-table-column>

          </el-table>
          <!-- 分页器 -->
          <el-pagination
            v-show="!isToggleStatus"
            :disabled="isToggleStatus"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="form.page"
            :page-size="form.pageSize"
            :page-sizes="[10, 20, 30, 40,50,60,70,80,90,100]"
            layout="sizes, prev, pager, next,jumper"
            :total="form.total">
          </el-pagination>
        </el-tab-pane>
        <el-tab-pane label="数据绘图" name="2">
          <span slot="label"><i class="el-icon-s-marketing"></i> 数据绘图</span>
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="总绘图" name="testAll">
              <el-card class="card_echarts" shadow="hover">
                <echarts-seven-compose
                  ref="testAll"
                  type="testAll"
                ></echarts-seven-compose>
              </el-card>
            </el-tab-pane>
            <el-tab-pane :name="item.text" :label="item.name" v-for="(item) in tabPanes" :key="item.text">
              <el-card class="card_echarts" shadow="hover">
                <echarts-seven-compose ref="tagItem" :type="item.text" :key="item.text">
                </echarts-seven-compose>
              </el-card>
            </el-tab-pane>
          </el-tabs>
        </el-tab-pane>

      </el-tabs>

      <!-- 新增编辑弹窗 -->
      <el-dialog
        :title="dialogTitle"
        :visible.sync="isShowDialog"
        :before-close="handleClose"
        :close-on-click-modal="false">
        <echarts-one-dialog ref="echartsDialog"></echarts-one-dialog>
      </el-dialog>
      <monitor-remark ref="monitorRemark" @update="$emit('update')"></monitor-remark>
    </div>
  </div>
</template>

<script>
  import EchartsSevenCompose2 from "../monitor-echarts/base/echarts-7-compose2";
  import EchartsSevenCompose from "../monitor-echarts/base/echarts-7-compose";
  import ItemMixins from "./item.mixins";
  import Format from '@/utils/format'

  export default {
    mixins: [ItemMixins],
    // components: {EchartsSevenCompose, EchartsSevenCompose2},
    components: {EchartsSevenCompose},
    props: ["typeOneUmumList", "calcType", "calcVal"],
    data() {
      return {
        activeName2: "2",
        tabPanes: [],
        isTestAll: ' ',
        isTest1: true,
        isTest2: true,
        tabsName: [],
        isEcharts: 0,
        activeName: "testAll",
        monitoringsTime: [],
        // 当前的图表钩子
        chartsName: ['testAll',],
        // 分页参数
        form: {
          page: 1,
          pageSize: 12,
          total: 0,
        },
        // 弹窗title
        dialogTitle: "",
        // 是否显示弹窗
        isShowDialog: false,
      };
    },
    filters: {
      // 时间格式化
      formatTimer(timer) {
        return Format(new Date(timer))
      },
    },
    computed: {
      // 筛选类型改变
      detailList() {
        return (
          this.typeOneUmumList.find((x) => x.code === this.calcTypeModel)
            ?.relationTypeList || []
        );
      },
      calcTypeModel: {
        get() {
          return this.calcType;
        },
        set(value) {
          this.$emit("update:calcType", value);
        },
      },
      calcValModel: {
        get() {
          return this.calcVal;
        },
        set(value) {
          this.$emit("update:calcVal", value);
        },
      },
    },
    methods: {
      //tab 切换进行echart随着窗口改变方法，让数据重新渲染，
      clickTab() {
        this.chartsName.map((name) => {
          if (this.$refs[name]) {
            this.$nextTick(() => {
              this.$refs[name].echartResize();
            });
          }
        });

      },
      setWarningLine() {
      },
      //

      //tab 切换进行echart随着窗口改变方法，让数据重新渲染，
      handleClick(tab, event) {
        this.isTestAll = tab.index - 1
        this.$nextTick(() => {
          this.$refs.tagItem[this.isTestAll].echartResize()
        })
        this.$nextTick(() => {
          this.$refs.testAll.echartResize()
        });
      },
      // 筛选类型改变
      typeChange(value) {
        this.$nextTick(() => {
          this.calcValModel = this.detailList[0]?.code;
        });
      },
      // 数据刷新
      update({data, monitorName, monitorInfo}) {
        this.equipId = monitorInfo.bindEquipId
        this.collectionList = data.recordList || []
        this.form.page = 1;
        this.form.total = this.collectionList.length;
        this.selectionList = JSON.parse(JSON.stringify(this.tableList));
        this.tabsName = []
        this.monitoringsTime = []
        if (data.paintList.length !== 0) {
          let sersorNames = this.selectionList[0].dataList
          sersorNames.forEach((item) => {
            this.tabsName.push(item.sensorName)
          })
          this.tabsName.splice(0, 1)
          this.chartsName = ['testAll']
          //遍历出tabs
          this.tabPanes = []
          let item = this.selectionList[0].dataList
          for (let j = 1; j < item.length; j++) {
            this.tabPanes.push({
              name: item[j].sensorName,
              text: 'text' + item[j].index
            })
            this.chartsName.push(
              'text' + item[j].index
            )
          }
          this.$refs.testAll.updateEchart(data.paintList, monitorName, data.createTime, '基准点', 0)
          // this.$refs.laser.updateEchart(data.paintList, monitorName, data.createTime, '基准点', 0)

          for (let i = 0; i < this.tabPanes.length; i++) {
            let a = this.tabPanes[i]
            this.$nextTick(() => {
              this.$refs.tagItem[i].updateEchart(data.paintList, monitorName, data.createTime, a, i + 1)
            })

          }
        } else if (data.paintList.length === 0) {
          this.tabsName = []
          this.tabPanes = []
          // this.$refs.laser.echartClear()
          this.$refs.testAll.echartClear()
        }
      },
      // 勾选改变
      handleSelectionChange(val) {
        this.selectionList = val;
      },
      handleClose() {
        this.isShowDialog = false;
      },
    },
    mounted() {

    }
  };
</script>

<style scoped="scoped" lang="less">
  @import url("./commom.less");

  // 还需自定义的css
  .card_echarts {
    height: 550px;
  }

  .monitor_page-left {
    // 15px未margin
    /*min-height: 1530px;*/
    height: 550px;
  }

  .filter_select {
    .el-select {
      width: 40%;
    }
  }


  .tabTitle {
    background: red;
  }
</style>
